use "$project_path/data/4_regdata/regdata", clear

log using "$project_path/output/appendix_tables/Appendix_Table_E10.smcl", replace

local variables = "corruption ex_corruption ex_bribe ex_embez ex_respect_con pub_corruption ex_cor_exch ex_theft accountability veracc diagacc horacc burocrat_merit impartial_admin bureaucracy_index control_corruption gov_effective reg_quality rule_law corruption_gici"
local negative_variables = "corruption ex_corruption ex_bribe ex_embez pub_corruption ex_cor_exch ex_theft corruption_gici"
foreach v in `negative_variables' {
	cap gen mstd_diff_m1_`v' = -std_diff_m1_`v'
}

foreach v in `variables' {
	if strpos("`negative_variables'", "`v'"){
		rdrobust mstd_diff_m1_`v' runvar, all
	}
	else {
		rdrobust std_diff_m1_`v' runvar, all
	}
}

log close

*** Building .tex table
// Regressions 
#delimit ;
local outcomes  
		mstd_diff_m1_corruption mstd_diff_m1_ex_corruption mstd_diff_m1_ex_bribe mstd_diff_m1_ex_embez std_diff_m1_ex_respect_con mstd_diff_m1_pub_corruption mstd_diff_m1_ex_cor_exch mstd_diff_m1_ex_theft std_diff_m1_accountability std_diff_m1_veracc std_diff_m1_diagacc std_diff_m1_horacc std_diff_m1_burocrat_merit std_diff_m1_impartial_admin std_diff_m1_bureaucracy_index std_diff_m1_control_corruption std_diff_m1_gov_effective std_diff_m1_reg_quality std_diff_m1_rule_law mstd_diff_m1_corruption_gici;
#delimit cr		

// Initialize matrix of results and define value labels
local ncols = 5
local nrows = 20
//Prepare matrix of coefficients and other statistics
mat Coefs = J(`nrows', `ncols', .)

mat list Coefs

//Inizialize indices
local row = 0

foreach v in `outcomes'{
	//Initialize indices
	local ++row

	rdrobust `v' runvar, all
	
	//Store estimates and stats
	local b_`row': di %5.3f e(tau_cl)
	local se_`row': di %5.3f e(se_tau_rb)
	local p_`row': di %5.3f e(pv_rb)
	local N_`row': di %6.0f e(N)

	mat Coefs[`row',1] = `b_`row''
	mat Coefs[`row',2] = `se_`row''
	mat Coefs[`row',3] = `p_`row''
	mat Coefs[`row',4] =  `N_`row''
	mat Coefs[`row',5] = .
	
	// Extract and store sources
    local basevar = "`v'"
	local basevar = subinstr("`basevar'", "mstd_", "", .)
	local basevar = subinstr("`basevar'", "std_", "", .)
	local basevar = subinstr("`basevar'", "diff_m1_", "", .)
	local source`row' = subinstr("``basevar'[note1]'", "Source - ", "", .)
}
mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
	// Clear the dataset, and use the matrix as dataset			

	clear
	svmat Coefs

	//	input variable names
	gen varname = ""
	gen temp = _n
	replace varname = "(Minus) Political corruption" if temp == 1
	replace varname = "(Minus) Executive corruption index" if temp == 2
	replace varname = "(Minus) Executive bribery" if temp == 3
	replace varname = "(Minus) Executive embezzlement" if temp == 4
	replace varname = "Executive respects constitution" if temp == 5
	replace varname = "(Minus) Public sector corruption index" if temp == 6
	replace varname = "(Minus) Public sector corrupt exchanges" if temp == 7
	replace varname = "(Minus) Public sector theft" if temp == 8
	replace varname = "Government accountability index" if temp == 9
	replace varname = "Vertical accountability index" if temp == 10
	replace varname = "Diagonal accountability" if temp == 11
	replace varname = "Horizontal accountability index" if temp == 12
	replace varname = "Bureaucrat merit" if temp == 13
	replace varname = "Impartiality of the administration" if temp == 14
	replace varname = "Bureaucracy quality index" if temp == 15
	replace varname = "Control of corruption" if temp == 16
	replace varname = "Government effectiveness" if temp == 17
	replace varname = "Regulatory quality" if temp == 18
	replace varname = "Rule of law" if temp == 19
	replace varname = "(Minus) Corruption index" if temp == 20
	
	order varname, first
	drop temp
	
	// Rename cols and labels
	forvalues n = 1/5{
		rename Coefs`n' Col`n'
	}
	
	label var varname ""
	label var Col1 "Est."
	label var Col2 "SE"
	label var Col3 "p-val"
	label var Col4 "N"
	label var Col5 "Source"
	
		
	// Convert to string and add options
	foreach i in 1 2 4 5{
		tostring Col`i', replace force format(%04.3f)
		replace Col`i'= "" if Col`i'=="."
	}
	
	// Sources
	forvalues i =1/`nrows' {
		replace Col5 = "`source`i''" in `i'
	}
	
	//Remove decimals to N obs
	replace Col4 = substr(Col4, 1, strpos(Col4, ".")-1)
		
	// Add stars
	local 3_star 0.01
	local 2_star 0.05
	local 1_star 0.10
	
	replace Col1 = Col1 + "***" if Col3 < `3_star'
	replace Col1 = Col1 + "**" if Col3 < `2_star' & Col3 > `3_star'
	replace Col1 = Col1 + "*" if Col3 < `1_star' & Col3 > `2_star'
	
    tostring Col3, replace force format(%04.3f)
    replace Col3= "" if Col3=="."
	
	// Add parenthesis to SE
	replace Col2 = "(" + Col2 + ")" if Col2 != ""
	
	// Add brackets to p-values
	replace Col3 = "[" + Col3 + "]" if Col3 != ""

	foreach var of varlist _all {
	qui replace `var' = ustrtrim(`var')	
}

local title = "Effects on additional governance variables"
local filename = "governance_variables_baseline"
local output "$project_path/output/appendix_tables/Appendix_Table_E10.tex"
local footnote = "\caption*{\footnotesize \emph{Notes}: This table reports RD estimates corresponding to equation (\ref{eq:baseline_rdd}) for measures of the quality of governance, expressed in standard deviation terms.}"


	texsave using "`output'", replace title("`title'") varlabels noendash ///
							  footnote("`footnote'") align(p{6.8cm} p{1.6cm} p{1.6cm} p{1.6cm} p{1.6cm} p{2.8cm}) ///
							  frag nofix marker("tab:`filename'") location(H)
